﻿using ConfExpert.Properties;
using ConfExpert.Utility;
using DevTeam;
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class Ruler : UserControl
    {
        public Ruler()
        {
            InitializeComponent();
        }

        Size InitialControlSize = new Size();
        Bitmap Cetvel;
        private Bitmap kumas = Resources.coming_soon;

        public Bitmap Kumas
        {
            get { return kumas; }
            set
            {
                kumas = value;
                //ReDraw();
            }
        }
        public int ProductId = 0;

        float kumasW;
        float kumasH;
        int cW;
        int cH;
        Double oran = 1;

        protected override void OnPaint(PaintEventArgs pe)
        {
            Cetvel = Resources.Ruler;

            float[][] matrixItems ={ 
                new float[] {1, 0, 0, 0, 0},
                new float[] {0, 1, 0, 0, 0},
                new float[] {0, 0, 1, 0, 0},
                new float[] {0, 0, 0, 0.7f, 0}, 
                new float[] {0, 0, 0, 0, 1}};
            ColorMatrix colorMatrix = new ColorMatrix(matrixItems);

            // Create an ImageAttributes object and set its color matrix.
            ImageAttributes imageAtt = new ImageAttributes();
            imageAtt.SetColorMatrix(
               colorMatrix,
               ColorMatrixFlag.Default,
               ColorAdjustType.Bitmap);

            pe.Graphics.DrawImage(Kumas, KumasLocation.X, KumasLocation.Y, kumasW, kumasH);

            CetvelArea = new Rectangle(CetvelLocation.X, CetvelLocation.Y, cW, cH);
            pe.Graphics.DrawImage(Cetvel, CetvelArea);
        }

        private void Ruler_Resize(object sender, EventArgs e)
        {
            ReDraw();
        }

        public void ReDraw()
        {
            if (InitialControlSize.Width == 0 || InitialControlSize.Height == 0)
                return;

            Double genislikDegisimi = Convert.ToDouble(this.Width) / Convert.ToDouble(InitialControlSize.Width);
            Double yukselikDegisimi = Convert.ToDouble(this.Height) / Convert.ToDouble(InitialControlSize.Height);
            oran = genislikDegisimi < yukselikDegisimi ? genislikDegisimi : yukselikDegisimi;

            KumasLocation.X = (this.Width - Convert.ToInt32(Kumas.Width * oran)) / 2 -10;
            KumasLocation.Y = (this.Height - Convert.ToInt32(Kumas.Height * oran)) / 2 -10;
            
            if (oran < 1)
                oran = 0.92;
            
            kumasW = (float)(Kumas.Width * oran);
            kumasH = (float)(Kumas.Height * oran);
            cW = Convert.ToInt32(Cetvel.Width * oran);
            cH = Convert.ToInt32(Cetvel.Height * oran);

            CetvelLocation.Y = Convert.ToInt32(KumasLocation.Y + kumasH - cH);
            CetvelLocation.X = KumasLocation.X;
            CetvelFirstLocation = CetvelLocation;

            this.Refresh();
        }

        private void Ruler_Load(object sender, EventArgs e)
        {
            Cetvel = Resources.Ruler;

            kumasW = 400;
            kumasH = 400;
            cW = Cetvel.Width;
            cH = Cetvel.Height;

            KumasLocation = new Point(0, 0);

            InitialControlSize.Width = this.Width;
            InitialControlSize.Height = this.Height;

            CetvelLocation.Y = Convert.ToInt32(KumasLocation.Y + kumasH - cH);
            CetvelLocation.X = KumasLocation.X;

            CetvelArea = new Rectangle(CetvelLocation.X, CetvelLocation.Y, Cetvel.Width, Cetvel.Height);

            CetvelFirstLocation = CetvelLocation;

            this.Refresh();
        }


        private Point offset = Point.Empty;
        Point CetvelLocation = new Point(0, 0);
        Point KumasLocation = new Point(0, 0);
        Point CetvelFirstLocation = new Point(0, 0);
        Rectangle CetvelArea = new Rectangle();

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (CetvelArea.Contains(e.Location))
            {
                this.Cursor = Cursors.NoMove2D;
            }
            else
            {
                this.Cursor = Cursors.Default;
            }

            if (offset != Point.Empty)
            {
                this.Cursor = Cursors.NoMove2D;
                CetvelLocation.X = CetvelFirstLocation.X + e.X - offset.X;
                CetvelLocation.Y = CetvelFirstLocation.Y + e.Y - offset.Y;
                this.Refresh();
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Button == MouseButtons.Left && CetvelArea.Contains(e.Location))
            {
                offset = new Point(e.X, e.Y);

            }
        }

        int first = 0;
        int last = 0;

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            offset = Point.Empty;

            first = CetvelLocation.Y;
            last = this.Height - CetvelArea.Height;
            CetvelLocation.X = KumasLocation.X;
            CetvelLocation.Y = last;
            //timer.Enabled = true;

            Refresh();
            this.Cursor = Cursors.Default;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            int step = 15;
            if (first > last)
            {
                first = first - step;
                if (first < last)
                {
                    first = last;
                }

                CetvelLocation.Y = first;
            }
            else if (first < last)
            {
                first = first + step;
                if (first > last)
                {
                    first = last;
                }
                CetvelLocation.Y = first;
            }
            else
            {
                timer.Enabled = false;
            }

            Refresh();
        }

        private void Ruler_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                menu.Show(this, e.Location);
            }
        }

        private void miKaydet_Click(object sender, EventArgs e)
        {
            SaveImage(false, ImageFormat.Jpeg);
        }

        private void SaveImage(bool Original, ImageFormat f)
        {
            using (SaveFileDialog saver = new SaveFileDialog())
            {
                saver.FileName = "Article_" + ProductId.ToString();
                saver.Filter = "Article Images|" + (f == ImageFormat.Jpeg ? "*.jpg" : ".png");
                if (saver.ShowDialog() == DialogResult.OK)
                {
                    byte[] image = GetImage(Original);
                    FileManager.Write(saver.FileName, image);

                    AppSession.MainForm.NotifyMain("Kumaş resmi başarıyla kaydedildi.");
                }
            }
        }

        private void resmiKopyalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CopyImage(false);
        }

        private void CopyImage(bool Original)
        {
            byte[] image = GetImage(Original);
            Clipboard.SetImage(image.ToBitmap());
            AppSession.MainForm.NotifyMain("Kumaş resmi başarıyla panoya kopyalandı.");
        }

        private byte[] GetImage(bool Original)
        {
            DataTable dt = Session.DataManager.ExecuteDataTable("GetProductPhoto", ProductId, Original ? 1 : 0);
            return (byte[])dt.Rows[0][1];
        }

        private void orjinalResmiKopyalaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CopyImage(true);
        }

        private void orjinalResmiKaydetToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveImage(true, ImageFormat.Png);
        }
    }
}
